package com.lemon.cloud.admin.web;

import com.lemon.cloud.admin.dto.TaskInfoDTO;
import com.lemon.cloud.admin.dto.TaskInfoParamDTO;
import com.lemon.cloud.admin.entity.SysFileUploadTask;
import com.lemon.cloud.admin.service.SysFileUploadTaskService;
import com.lemon.cloud.comm.model.ResultMsg;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/file")
@Tag(name = "分片文件接口")
public class SysSpliceFileController {

    @Autowired
    private SysFileUploadTaskService sysFileUploadTaskService;

    /**
     * 获取上传进度
     *
     * @param identifier
     * @return
     */
    @Operation(
            summary = "获取上传进度", description = "获取上传进度",
            parameters = @Parameter(description = "获取上传进度", required = true,
                    schema = @Schema(type = "string", format = "string")),
            responses = {
                    @ApiResponse(responseCode = "200", description = "successful operation",
                            content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
                                    schema = @Schema(implementation = TaskInfoDTO.class))}, useReturnTypeSchema = true),
                    @ApiResponse(responseCode = "400", description = "参数错误")
            }
    )
    @GetMapping("/process/{identifier}")
    public ResultMsg<TaskInfoDTO> taskInfo(@PathVariable("identifier") String identifier) {
        return ResultMsg.resultSuccess(sysFileUploadTaskService.getTaskInfo(identifier));
    }

    /**
     * 创建一个上传任务
     *
     * @return
     */
    @Operation(
            summary = "创建一个上传任务", description = "创建一个上传任务",
            parameters = @Parameter(description = "创建一个上传任务", required = true,
                    schema = @Schema(type = "string", format = "string")),
            responses = {
                    @ApiResponse(responseCode = "200", description = "successful operation",
                            content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
                                    schema = @Schema(implementation = TaskInfoDTO.class))}, useReturnTypeSchema = true),
                    @ApiResponse(responseCode = "400", description = "参数错误")
            }
    )
    @PostMapping("/createTask")
    public ResultMsg<TaskInfoDTO> createTask(@Parameter @Valid @RequestBody TaskInfoParamDTO param) {
        return ResultMsg.resultSuccess(sysFileUploadTaskService.createTask(param));
    }

    @Operation(
            summary = "获取每个分片的预签名上传地址", description = "获取每个分片的预签名上传地址",
            parameters = @Parameter(description = "获取每个分片的预签名上传地址", required = true,
                    schema = @Schema(type = "string", format = "string")),
            responses = {
                    @ApiResponse(responseCode = "200", description = "successful operation",
                            content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
                                    schema = @Schema(implementation = TaskInfoDTO.class))}, useReturnTypeSchema = true),
                    @ApiResponse(responseCode = "400", description = "参数错误")
            }
    )
    @GetMapping("/process/{identifier}/{partNumber}")
    public ResultMsg preSignUploadUrl(@PathVariable("identifier") String identifier,
                                      @PathVariable("partNumber") Integer partNumber) {
        SysFileUploadTask task = sysFileUploadTaskService.getByIdentifier(identifier);
        if (task == null) {
            return ResultMsg.resultFail("分片任务不存在");
        }
        Map<String, String> params = new HashMap<>();
        params.put("partNumber", partNumber.toString());
        params.put("uploadId", task.getUploadId());
        return ResultMsg.resultSuccess(sysFileUploadTaskService.genPreSignUploadUrl(task.getBucketName(), task.getFileName(), params));
    }

    /**
     * 合并分片
     *
     * @param identifier
     * @return
     */
    @PostMapping("/process/merge/{identifier}")
    public ResultMsg merge(@PathVariable("identifier") String identifier) {
        sysFileUploadTaskService.merge(identifier);
        return ResultMsg.resultSuccess();
    }
}
